package com.dupengcheng.subject.application.controller;


import com.alibaba.fastjson.JSONObject;
import com.dupengcheng.subject.application.convert.SubjectAnswerDTOConvert;
import com.dupengcheng.subject.application.convert.SubjectInfoDTOConvert;
import com.dupengcheng.subject.application.dto.SubjectCategoryDTO;
import com.dupengcheng.subject.application.dto.SubjectInfoDTO;
import com.dupengcheng.subject.common.entity.PageResult;
import com.dupengcheng.subject.common.entity.Result;
import com.dupengcheng.subject.domain.entity.SubjectAnswerBO;
import com.dupengcheng.subject.domain.entity.SubjectInfoBO;
import com.dupengcheng.subject.domain.service.SubjectCategoryDomainService;
import com.dupengcheng.subject.domain.service.SubjectDomainService;
import com.google.common.base.Preconditions;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RequestMapping("/subject")
@RestController
@Slf4j
public class SubjectController {



    @Resource
    private SubjectDomainService subjectDomainService;


    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody SubjectInfoDTO subjectInfoDTO){
        try{
            if (log.isInfoEnabled()){
                log.info("SubjectController.add.dto:{}",
                        JSONObject.toJSONString(subjectInfoDTO));
            }

            SubjectInfoBO subjectInfoBO = SubjectInfoDTOConvert.INSTANCE
                    .convertDTOToBO(subjectInfoDTO);
            List<SubjectAnswerBO> subjectAnswerBOS = SubjectAnswerDTOConvert.INSTANCE
                    .convertDTOToBO(subjectInfoDTO.getOptionList());
            subjectInfoBO.setOptionList(subjectAnswerBOS);
            subjectDomainService.add(subjectInfoBO);
            return Result.ok(true);

        }catch (Exception e){

            log.error("SubjectController.add.error:{}",e.getMessage(),e);
            return Result.fail(e.getMessage());

        }
    }
    @PostMapping("/query")
    public Result<PageResult<SubjectInfoDTO>> query(@RequestBody SubjectInfoDTO subjectInfoDTO){
        try{
            if (log.isInfoEnabled()){
                log.info("SubjectController.query.dto:{}",
                        JSONObject.toJSONString(subjectInfoDTO));
            }


            SubjectInfoBO subjectInfoBO = SubjectInfoDTOConvert.INSTANCE
                    .convertDTOToBO(subjectInfoDTO);
            PageResult<SubjectInfoBO> subjectInfoBOList=subjectDomainService.query(subjectInfoBO);

            return Result.ok(subjectInfoBOList);

        }catch (Exception e){

            log.error("SubjectController.add.error:{}",e.getMessage(),e);
            return Result.fail(e.getMessage());

        }

    }

    @PostMapping("/querySubjectDetail")
    public Result<SubjectInfoDTO> querySubjectDetail(@RequestBody SubjectInfoDTO subjectInfoDTO){
        try{
            if (log.isInfoEnabled()){
                log.info("SubjectController.querySubjectDetail.dto:{}",
                        JSONObject.toJSONString(subjectInfoDTO));
            }


            SubjectInfoBO subjectInfoBO = SubjectInfoDTOConvert.INSTANCE
                    .convertDTOToBO(subjectInfoDTO);
            SubjectInfoBO subjectInfoBOList=subjectDomainService.querySubjectDetail(subjectInfoBO);

            return Result.ok(subjectInfoBOList);

        }catch (Exception e){

            log.error("SubjectController.add.error:{}",e.getMessage(),e);
            return Result.fail(e.getMessage());

        }

    }
}
